<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;

class User extends Controller
{
    public function index()
    {
//        $list = Db::name('user_information')->field('id,icon,username,sex,phone,qq,email,regtime,logtime,regip,status')->select();
        //判断是否为post请求
        if(request()->isPost()){
            $wwb = input("post.uname");
        }else{
            $wwb = cookie('uname');

        }

        if(empty($wwb)){
            //删除cooke
            cookie('uname', null);
            //查询数据

            $list = Db::name('user')
                    ->field(['user_id','user_tel','user_email','user_regtime','user_status'])
                    ->paginate(2);
        }else{
            //添加cookie
            cookie('uname', $wwb, 3600);
            //查询数据
            $list = Db::field('job_user.id uid,username,sex,user_tel,user_email,user_regtime,regip,user_status')
                            ->table('job_user, job_usermore_information')
                            ->where('username','like',"%$aaa%")
                            ->paginate(3);

        }

        //遍历用户信息
        $data = array();
        foreach($list as $v){
            if ($v['user_status'] == 1){
                $v['user_status'] = '已启用';
            }else{
                $v['user_status'] = '已停用';
            }
            $uid = $v['user_id']; //获取用户id
            // 定义空数组
            $data[] = Db::name('usermore_information')->field('username,sex,regip')->where('uid='.$uid)->find();
        }

        $arr = array();
        foreach($data as $k=>$r){
            $arr[] = array_merge($r,$list[$k]);
        }

        $page = $list->render();
        $this->assign("page",$page);
        $this->assign('list',$arr);
        return view('user/index');
    }

    public function del()
    {
        return view('user/del');
    }

    public function add()
    {
        return view('user/add');
    }

    public function doadd()
    {
        $data = input('post.');

        if ($data['user_pwd'] !== $data['user_repwd']) {
            $this->error('两次密码不一致');
        }
        unset($data['user_repwd']);
        $username = $data['username'];
        unset($data['username']);
        $data['user_pwd'] = password_hash($data['user_pwd'],PASSWORD_DEFAULT); ;
        $arr = $data;
        $arr['user_regtime'] = time();
        $uid = Db::name('user')-> insertGetId($arr);

        $request = Request::instance();
        $regip = $request->ip();
        $list = array(
            "uid" => $uid,
            "regip" => $regip,
            "username" => $username,
        );
        $ret = Db::name('usermore_information')->insertGetId($list);

        dump($ret);

    }

    public function userStatus1($id)
    {
        Db::name('user')->where('user_id', $id)
            ->update(['user_status' => '2']);
        return $id;
    }

    public function userStatus2($id)
    {
        Db::name('user')->where('user_id', $id)
            ->update(['user_status' => '1']);
        return $id;
    }

    public function resetpwd($id,$username)
    {

        $this->assign('uid',$id);
        $this->assign('name',$username);
        return view('user/pass');
    }

    public function dopwdset()
    {
        $data = input('post.');
        dump($data['pwd']);
    }

    public function roleMessage($id)
    {
        $data = Db::name('usermore_information')
                ->field('height,weight,health,brithday,school,education,admissiontime,jobintension,jobcity,jobexperience,selfdescribe,username')
                ->where('uid='.$id)->find();
        $this->assign('data',$data);
        return view('user/details');

    }

}